import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import ocsf.server.ConnectionToClient;


public class ServerHandler {
	private DBConnector dbc;
	
	public ServerHandler(DBConnector dbc){
		this.dbc = dbc;
	}
	
	/*
	 * Perform any Action received from client's Message
	 */
	public void doAction(String message, ConnectionToClient client){
		Query qry;
		Connection conn;
		
		switch(message){
		case "1": // Get list from DB and send to client
			conn = dbc.openConn();
			qry = new Query(true, "SELECT * FROM test_table;");
			ResultSet rs = dbc.runQuery(qry, conn);
			try {
				while(rs.next()){
					client.sendToClient(rs.getString(1));
				}
				rs.close();
			} catch (SQLException e) {} catch (IOException e) {}
			dbc.closeConn(conn);
			break;
		case "2": // Insert value to DB
			conn = dbc.openConn();
			qry = new Query(false, "INSERT INTO test_table VALUES ('B');");
			dbc.runQuery(qry, conn);
			try {
				client.sendToClient("DB Updated.");
			} catch (IOException e) {}
			dbc.closeConn(conn);
			break;
		default:
			try {
				client.sendToClient("Incorect Option!");
			} catch (IOException e) {}
			break;
		}
	}
}
